﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using AspNetCs.Config;
using System.Data;
using System.Data.SqlClient;
namespace AspNetCs.SQLServerDAL
{
    public class Province_DAL : IProvince
    {


        #region IProvince 成员

        /// <summary>
        /// 添加省份
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Province_Model model)
        {
            int Result = -1;
            db Cn = new db();
            try
            {
                string Sql = "insert into Province(ClassName) values('" + model.ClassName + "')";
                Cn.Execute_Noget(Sql);
                Result = 0;
            }
            catch (Exception ex)
            {
                new CustomException(ex);
            }
            finally
            {
                Cn.close();
            }
            return Result;
        }
        /// <summary>
        /// 删除省份
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int del(int id)
        {
            int Result = -1;
            db Cn = new db();
            try
            {
                Result = Cn.ExcSql("delete From Province Where id=" + id);
            }
            catch (Exception ex)
            {
                new CustomException(ex);
            }
            finally
            {
                Cn.close();
            }
            return Result;

        }


        public DataTable ProvinceList()
        {
            db Cnlist = new db();
            DataTable dt = Cnlist.GetSql_DataTable("Select * from Province order by Addtime desc");
            return dt;
        }
        public int update(Province_Model model)
        {

            int Result = -1;
            db Cn = new db();
            try
            {
                string Sql = "update Province Set Classname='" + model.ClassName + "' where id=" + model.Id + " ";
                Cn.Execute_Noget(Sql);
                Result = 0;
            }
            catch (Exception ex)
            {
                new CustomException(ex);
            }
            finally
            {
                Cn.close();
            }
            return Result;

        }
        public void UpdateOrder(int id)
        {
            db Cn = new db();
            string Sql = "update Province Set Addtime='" + DateTime.Now + "' where id=" + id + " ";
            Cn.Execute_Noget(Sql);
        }
        /// <summary>
        /// 根据SQL语句,返回省份信息
        /// </summary>
        /// <param name="filter">WHERE条件</param>
        /// <returns></returns>
        public Province_Model GetModel(string filter)
        {
            Province_Model model = new Province_Model();
            db Cn = new db();
            try
            {
                SqlDataReader sdr = Cn.GetSqlDataReader("Select Top 1 * from Province where id<>0 " + filter);
                while (sdr.Read())
                {
                    model.Id = sdr.GetInt32(0);
                    model.ClassName = sdr["ClassName"].ToString();
                    model.Addtime = Convert.ToDateTime(sdr["Addtime"]);
                }
            }
            catch (Exception Ex)
            {
                new CustomException(Ex);
            }
            finally
            {
                Cn.close();
            }
            return model;

        }

        #endregion
    }
}
